package com.klasster.recruitmentPlatform.common.recommendation;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.klasster.recruitmentPlatform.domain.po.Company;
import com.klasster.recruitmentPlatform.domain.po.Job;
import com.klasster.recruitmentPlatform.domain.po.JobIntention;
import com.klasster.recruitmentPlatform.domain.vo.CompanyHomeVO;
import com.klasster.recruitmentPlatform.domain.vo.JobBriefVO;
import com.klasster.recruitmentPlatform.domain.vo.JobHomeVO;
import com.klasster.recruitmentPlatform.service.impl.CompanyServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.JobIntentionServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.JobServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class CompanyRecommendationUtil {

    private final JobIntentionServiceImpl jobIntentionService;
    private final JobServiceImpl jobService;
    private final CompanyServiceImpl companyService;

    public List<CompanyHomeVO> getRecommendCompanies(Long seeker_id, Integer amount) {
        // 1.获取求职意向
        List<JobIntention> jobIntentionList = jobIntentionService.getBySeekerID(seeker_id);
        int size = jobIntentionList.size();
        if(size==0) {
            return getCommonRecommendCompanies(amount);
        }

        // 2.计算每个求职意向平均应该获取多少个推荐企业
        int avg = amount / size;
        int res = amount % size;

        // 2.根据求职意向，给出企业推荐
        List<Company> list = new LinkedList<>();
        for (JobIntention e : jobIntentionList) {
            String industryCode = e.getIndustry_code();
            QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
            int plus = res > 0 ? 1 : 0;
            queryWrapper.eq("industry_code", industryCode).eq("state",1).last("limit " + (avg + plus));
            List<Company> temp = companyService.list(queryWrapper);
            //检查该求职意向是否能获取到预期数量的企业推荐
            if (temp.size() == avg + plus)
                res--;
            else
                res += avg + plus - temp.size();
            list.addAll(temp);
        }
        // 3.从求职意向中获取的推荐企业不足amount，获取最新的企业推荐进行补充
        if(res>0) {
            //去除已有的company_id
            List<Long> neq = new LinkedList<>();
            list.forEach(e -> neq.add(e.getCompany_id()));
            list.addAll(getCommonRecommendCompanies(res,neq));
        }
        List<CompanyHomeVO> companies = BeanUtil.copyToList(list, CompanyHomeVO.class);
        setCompanyJobs(companies);
        return companies;
    }

    private List<Company> getCommonRecommendCompanies(Integer amount, List<Long> neq) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        neq.forEach(e -> queryWrapper.ne("company_id", e));
        queryWrapper.eq("state",1).last("limit "+amount);
        return companyService.list(queryWrapper);
    }

    public List<CompanyHomeVO> getCommonRecommendCompanies(Integer amount) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",1).last("limit "+amount);
        List<Company> list = companyService.list(queryWrapper);
        List<CompanyHomeVO> companies = BeanUtil.copyToList(list, CompanyHomeVO.class);
        setCompanyJobs(companies);
        return companies;
    }

    /**
     * 获取该企业最近至多3个职位
     */
    private void setCompanyJobs(List<CompanyHomeVO> companies) {
        companies.forEach(e -> {
            QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("company_id", e.getCompany_id()).eq("state",1).last("limit 3");
            List<Job> jobs = jobService.list(queryWrapper);
            List<JobBriefVO> briefVOS = BeanUtil.copyToList(jobs, JobBriefVO.class);
            e.setJobs(briefVOS);
        });
    }


}
